﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Microsoft.Reporting.WebForms;

namespace AcadPosApp.Relatorios
{
    public partial class gerarRelatorio : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnMatriz_Click(object sender, EventArgs e)
        {
            DataTable dtImp = ControlClass.CNMatrizCurricular.getRelMatrizCurricular(4, 20142, 3);
            if (dtImp.Rows.Count == 0)
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Alert", "bootbox.alert('Não há registros à serem impressos!', function() {});", true);
            }
            else
            {
                ReportViewer ReportViewer1 = new ReportViewer();
                ReportViewer1.Visible = true;
                ReportViewer1.ProcessingMode = ProcessingMode.Local;
                LocalReport relatorio = ReportViewer1.LocalReport;
                relatorio.ReportPath = Server.MapPath(@"relMatrizCurricular.rdlc");
                ReportViewer1.Reset();

                ReportDataSource rds1 = new ReportDataSource();
                rds1.Name = "DataSet1";
                rds1.Value = dtImp;

                ReportDataSource rds2 = new ReportDataSource();
                rds2.Name = "DataSet2";
                rds2.Value = ControlClass.CNParametros.getCabecalhoRelatorio();


                //#region Preenchendo Dados do Cabeçalho
                //string Nome_Associcao = dt.Rows[0]["NOME_ASSOCIACAO"].ToString();
                //string Nome_Instituicao = dt.Rows[0]["NOME_INSTITUICAO"].ToString();
                //string Endereco = dt.Rows[0]["ENDERECO"].ToString();
                //string Autorizacao_Portaria = dt.Rows[0]["AUTORIZACAO_PORTARIA"].ToString();
                //string Home_Page = dt.Rows[0]["HOME_PAGE"].ToString();
                //Image LOGOMARCA = dt.Rows[0]["LOGOMARCA"].ToString();
                //#endregion

                //ReportParameter rp_Matricula = new ReportParameter("rp_Matricula", aluno.Matricula.ToString());
                //ReportParameter rp_Nome = new ReportParameter("rp_Nome", aluno.NomeCompleto);
                //ReportParameter rp_Curso = new ReportParameter("rp_Curso", aluno.CursoNome);
                //ReportParameter rp_Curriculo = new ReportParameter("rp_Curriculo", aluno.Curriculo.ToString());

                //DataTable dtDados = ControlClass.Graducao.CNMatricula.obterDadosPenultimaMatricula(aluno.Matricula);

                //ReportParameter rp_Periodo = new ReportParameter("rp_Periodo", dtDados.Rows[0]["Periodo"].ToString());
                //ReportParameter rp_Ano = new ReportParameter("rp_Ano", dtDados.Rows[0]["Ano"].ToString());
                //ReportParameter rp_Semestre = new ReportParameter("rp_Semestre", dtDados.Rows[0]["Semestre"].ToString());

                //#region Parametros do Cabeçalho
                //ReportParameter rp_Nome_Associcao = new ReportParameter("rp_Nome_Associcao", Nome_Associcao);
                //ReportParameter rp_Nome_Instituicao = new ReportParameter("rp_Nome_Instituicao", Nome_Instituicao);
                //ReportParameter rp_Endereco = new ReportParameter("rp_Endereco", Endereco);
                //ReportParameter rp_Autorizacao_Portaria = new ReportParameter("rp_Autorizacao_Portaria", Autorizacao_Portaria);
                //ReportParameter rp_Home_Page = new ReportParameter("rp_Home_Page", Home_Page);
                //#endregion

                //ReportParameter[] ReportParans = new ReportParameter[] { rp_Matricula, rp_Nome, rp_Curso, rp_Curriculo, rp_Nome_Associcao, rp_Nome_Instituicao, rp_Endereco, rp_Autorizacao_Portaria, rp_Home_Page, rp_Periodo, rp_Ano, rp_Semestre };

                //relatorio.SetParameters(ReportParans);
                relatorio.DataSources.Clear();
                relatorio.DataSources.Add(rds1);
                relatorio.DataSources.Add(rds2);
                relatorio.Refresh();

                #region Gerar PDF
                Warning[] warn = null;
                string[] streamids = null;
                string mimeType = "application/pdf";
                string encoding = string.Empty;
                string extension = string.Empty;
                byte[] byteViewer;

                byteViewer = relatorio.Render("pdf", null, out mimeType, out encoding, out extension, out streamids, out warn);
                Response.Buffer = true;
                Response.Clear();
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-disposition", "inline; filename=MatrizCurricular.pdf");
                Response.BinaryWrite(byteViewer);
                Response.Flush();

                Response.End();
                Response.ClearContent();
                #endregion
            }
        }
    }
}